package com.dick.base.api;

import com.dick.base.dto.DictValueTreeNode;
import com.dick.base.model.BaseDict;
import com.dick.base.parameter.DictParameter;
import com.dick.base.parameter.DictValueParameter;
import com.dick.base.service.DictService;
import com.dick.base.util.BaseResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 字典api
 * @desc    字典api，可以查询、修改、删除字典列表和字典值
 * @project 巡视与党风廉政建设系统
 * @company 北京远桥科技有限公司
 * @author  wanglei
 * @version 1.0
 * @date    2021-06-03
 */
@RequestMapping("api/dictionaries")
@RestController
public class DictApi {

    @Autowired
    private DictService dictService;

    @ApiOperation(value = "字典列表", tags = {"字典", "公共"}, notes = "返回全部数据，如果数据量较大，前端可自行处理")
    @GetMapping
    public BaseResult<List<BaseDict>> dictList() {
        return BaseResult.of(dictService.baseDictList());
    }

    @ApiOperation(value = "字典值列表树", tags = {"字典", "公共"})
    @GetMapping("{code}")
    public BaseResult<List<DictValueTreeNode>> baseDictValuesTree(@PathVariable String code) {
        return BaseResult.of(dictService.baseDictValuesTree(code));
    }

    @ApiOperation(value = "新增或修改字典", tags = {"字典", "公共"})
    @PostMapping
    public BaseResult<BaseDict> addOrModifyBaseDict(@RequestBody @Valid DictParameter dict) {
        return BaseResult.of(dictService.addOrModifyBaseDict(dict));
    }

    @ApiOperation(value = "新增或修改字典值", tags = {"字典", "公共"}, notes = "返回字典码新的字典值列表")
    @PostMapping("value")
    public BaseResult<List<DictValueTreeNode>> addOrModifyBaseDictValue(@RequestBody @Valid DictValueParameter value) {
        dictService.addOrModifyBaseDictValue(value);
        return BaseResult.of(dictService.baseDictAllValuesTree(value.getCode()));
    }

    @ApiOperation(value = "删除字典", tags = {"字典", "公共"}, notes = "不会级联删除字典值")
    @PostMapping("delete")
    public BaseResult<Void> deleteBaseDict(@RequestParam Long id) {
        dictService.deleteBaseDict(id);
        return BaseResult.voidResult();
    }

    @ApiOperation(value = "删除字典值", tags = {"字典", "公共"}, notes = "级联删除子字典值")
    @PostMapping("value/delete")
    public BaseResult<Void> deleteBaseDictValue(@RequestParam Long id) {
        dictService.deleteBaseDictValue(id);
        return BaseResult.voidResult();
    }
}
